
#!/bin/bash
. /etc/profile

mysql --socket=/tmp/mysql-blackhole.sock -e "flush logs" ;


TODAY=`date +"%Y%m%d"`
lastTEN=$(date "-d -10 minute" +"%Y-%m-%d_%H_%M")
user=root
passwd=6JfQcaaU0OoOyuMA810X5nTYCrm6g2kL
database=qyer_lastminute
src_dir=/Data/mysql-blackhole/log/
log_dir=/data3/logs/mysql/log/
wareinginfo=$log_dir"mysqldailybak.waraing"
dailylog=$log_dir"mysqldailybak.log"
binlog_dir=/data3/logs/mysql/binlog/
data_dir=/data3/logs/mysql/data/


echo "     " > $wareinginfo
echo "---------------------------------" >> $wareinginfo
echo $(date +"%y-%m-%d %H:%M:%S") >> $wareinginfo
echo "---------------------------------" >> $wareinginfo

StartTime=$(date "-d -10 minute" +"%Y-%m-%d %H:%M:%S")

filename=`ls /Data/mysql-blackhole/log |grep  mysql-blackhole-bin |grep -v mysql-blackhole-bin.index`

echo $filename
filename=${filename//" "/ };
arr=($filename)
len=${#arr[@]}
((len--))
unset arr[$len]

for i in ${arr[@]}  
do
	log_name=${i##*/}
	echo $log_name
	echo "$StartTime start backup binlog " >> $wareinginfo
	mysqlbinlog  --start-datetime="$StartTime" $src_dir$i  >> $dailylog"."$TODAY |tee -a $wareinginfo 
	mysqlbinlog  --start-datetime="$StartTime" $src_dir$i | mysql -u$user -p$passwd 
	mv $src_dir$i $binlog_dir$log_name
done
if [ $? = 0 ] ;then
 	#cd $binlog_dir
 	echo "daily backup succeed" >> $wareinginfo
else 
 	echo "daily backup fail" >> $wareinginfo
 	#mail -s "MySQL Backup" $eMail < $wareinginfo  #备份失败之后发送邮件通知
fi


#save data in file 

mkdir -p $data_dir$TODAY

mysql -u$user -p$passwd -e "
use qyer_lastminute ; 
select l.id,c.pid,c.id,c.price,c.house_diff,l.recommend,l.views,l.modifytime,c.total,l.sort_sold_num,l.sort_time,l.sort_weight,l.sort_list_price 
from lastminute_main l 
left join lastminute_category c on l.id = c.lid 
where l.status=0 and l.onsale = 1 
and unix_timestamp(now()) between l.start_time and l.end_time and c.total>0 and c.type = 0 
and TIMESTAMPDIFF(MINUTE,from_unixtime(l.modifytime),now()) <= 11" >> $data_dir$TODAY"/"$lastTEN".data"

if [ $? = 0 ] ;then
 	#cd $binlog_dir
 	echo "save data in file  succeed" >> $wareinginfo
else 
 	echo "save data in file  fail" >> $wareinginfo
 	#mail -s "MySQL Backup" $eMail < $wareinginfo  #备份失败之后发送邮件通知
fi

